import express from 'express';
import cors from 'cors';
import path from 'path';
import { createVideoTable } from '../app/models/video.model';
import videoRoutes from './routes/video.routes';
import questionRoutes from './routes/question.routes';
import tokenRoutes from '../app/routes/token.routes';
import callbackRoutes from '../app/routes/callback.routes';
import pptRoutes from '../app/routes/ppt.routes';
import docmeeRoutes from './routes/docmee.routes';

const app = express();
const port = process.env.PORT || 3002;

// CORS 配置
app.use(cors({
  origin: ['http://localhost:5173', 'http://localhost:3000', 'http://localhost:3001', 'http://localhost:3002'], // 允许的前端域名
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Api-Key', 'token', 'x-signature'],
  credentials: true,
  maxAge: 86400 // 24小时
}));

app.use(express.json());

// 静态文件服务
app.use('/uploads', express.static(path.join(__dirname, '../uploads')));
app.use('/output', express.static(path.join(__dirname, '../output')));

// 路由
app.use('/api', videoRoutes);
app.use('/api', questionRoutes);
app.use('/api/user', tokenRoutes);
app.use('/api/callback', callbackRoutes);
app.use('/api/ppt', pptRoutes);
app.use('/api/docmee', docmeeRoutes);

// 初始化数据库
createVideoTable().catch(console.error);

// 启动服务器
app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
}); 